#coding: utf-8
import sys
from selenium import webdriver
import re
from datetime import datetime

brand_re = re.compile('鍝佺墝:.*?\<\/li\>')

def taobao_url(pid):
    return "https://detail.taobao.com/item.htm?id=%s"%pid
def tmall_url(pid):
    return "https://detail.tmall.com/item.htm?id=%s"%pid

pid_file = open('pid.csv')

browser = webdriver.Chrome()
outfile = datetime.now().strftime("%Y%m%d%H%M%S.log")

#write_file = open('pid_with_brand.csv', 'a',)
write_file = open(outfile, 'w',)


line = pid_file.readline()
i = 0

for iter in range(93759):
    i += 1
    line = pid_file.readline()

while len(line) > 0:
    i += 1
    sys.stdout.write('\r'+str(i))
    sys.stdout.flush()
    line = pid_file.readline()
    line = line.replace('"', '')
    segments = line.split(',')
    if len(segments) < 2:
        break
    brand = ''
    if segments[1] == 'taobao_b':
        pid = segments[6]
        url = taobao_url(pid)
        browser.get(url)
        try:
            brand = brand_re.findall(browser.page_source)[0][4:-5]
        except:
            pass
    sys.stdout.write(' ' + brand + line)
    write_file.write(brand + line)

write_file.close()
pid_file.close()
browser.close()
